﻿#region Copyright ©2008-2010, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: kalus $
// Last Changed Date     $LastChangedDate: 2009-05-15 17:00:33 +0200 (Fri, 15 May 2009) $
// Last Changed Revision $Rev: 141 $
//
// ====================================================================================================
#endregion

using System.Windows;
using System.Windows.Media;
using Microsoft.Win32;

namespace Tum.CollabXT.Simulator
{
    /// <summary>
    /// Interaction logic for SharePointConfigPage.xaml
    /// </summary>
    public partial class SimulatorConfigPage : ToolProviderConfigPage
    {
        #region Private Attributes
        private IConversionWorkflow _conversionWorkflow;
        private SimulatorToolProvider _toolProvider;
        #endregion

        public SimulatorConfigPage()
        {
            InitializeComponent();
        }

        public override void ReceiveProviderHandle(IToolProvider toolProvider)
        {
            _toolProvider = toolProvider as SimulatorToolProvider;

            _toolProvider.OutputFile = Properties.Settings.Default.LastOutputFile;
            _toolProvider.ArchitectureFile = Properties.Settings.Default.LastArchitectureFile;

            UpdateControls();
        }

        public override void ReceiveWorkflowHandle(IConversionWorkflow workflow)
        {
            _conversionWorkflow = workflow;
        }

        private void UpdateControls()
        {
           //Output path
           if (string.IsNullOrEmpty(_toolProvider.OutputFile))
              lblOutputPathValue.Content = Simulator.Resources.Language.PathNotSet;
           else
           {
              lblOutputPathValue.Content = _toolProvider.OutputFile;
              lblOutputPathValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
           }

           //Architecture File
           if (string.IsNullOrEmpty(_toolProvider.ArchitectureFile))
              lblArchitecturePathValue.Content = Simulator.Resources.Language.PathNotSet;
           else
           {
              lblArchitecturePathValue.Content = _toolProvider.ArchitectureFile;
              lblArchitecturePathValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
           }

           //Specification File
           if (string.IsNullOrEmpty(_toolProvider.SpecificationFile))
              lblSpecificationPathValue.Content = Simulator.Resources.Language.PathNotSet;
           else
           {
              lblSpecificationPathValue.Content = _toolProvider.SpecificationFile;
              lblSpecificationPathValue.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
           }

           //Button states
           UpdateWorkflowButtons();
        }

        private void UpdateWorkflowButtons()
        {
            if (_toolProvider != null)
            {
               _conversionWorkflow.UpdateButtonState(true, !string.IsNullOrEmpty(_toolProvider.OutputFile));
            }
            else
            {
                _conversionWorkflow.UpdateButtonState(true, false);
            }
        }

        private void btnSetOutputPath_Click(object sender, RoutedEventArgs e)
        {
           SaveFileDialog sfd = new SaveFileDialog();

           if (!string.IsNullOrEmpty(Properties.Settings.Default.LastOutputFile))
           {
              string lastDirectory = System.IO.Path.GetDirectoryName(Properties.Settings.Default.LastOutputFile);
              if (System.IO.Directory.Exists(lastDirectory))
                 sfd.InitialDirectory = lastDirectory;
           }

           sfd.DefaultExt = "txt";
           sfd.Filter = "Text files (*.txt)|*.txt";

           bool? result = sfd.ShowDialog();
           if (result.HasValue && result.Value == true)
           {
              _toolProvider.OutputFile = sfd.FileName;
              _conversionWorkflow.InvalidateProcessing();
              UpdateControls();

              Properties.Settings.Default.LastOutputFile = sfd.FileName;
              Properties.Settings.Default.Save();
           }
        }

        private void btnSetArchitecturePath_Click(object sender, RoutedEventArgs e)
        {
           OpenFileDialog ofd = new OpenFileDialog();

           if (!string.IsNullOrEmpty(Properties.Settings.Default.LastArchitectureFile))
           {
              string lastDirectory = System.IO.Path.GetDirectoryName(Properties.Settings.Default.LastArchitectureFile);
              if (System.IO.Directory.Exists(lastDirectory))
                 ofd.InitialDirectory = lastDirectory;
           }

           ofd.DefaultExt = "xml";
           ofd.Filter = "XML files (*.xml)|*.xml";

           bool? result = ofd.ShowDialog();
           if (result.HasValue && result.Value == true)
           {
              _toolProvider.ArchitectureFile = ofd.FileName;
              _conversionWorkflow.InvalidateProcessing();
              UpdateControls();

              Properties.Settings.Default.LastArchitectureFile = ofd.FileName;
              Properties.Settings.Default.Save();
           }
        }

        private void btnSetSpecificationFile_Click(object sender, RoutedEventArgs e)
        {
           OpenFileDialog ofd = new OpenFileDialog();

           if (!string.IsNullOrEmpty(Properties.Settings.Default.LastSpecificationFile))
           {
              string lastDirectory = System.IO.Path.GetDirectoryName(Properties.Settings.Default.LastSpecificationFile);
              if (System.IO.Directory.Exists(lastDirectory))
                 ofd.InitialDirectory = lastDirectory;
           }

           ofd.DefaultExt = "xml";
           ofd.Filter = "XML files (*.xml)|*.xml";

           bool? result = ofd.ShowDialog();
           if (result.HasValue && result.Value == true)
           {
              _toolProvider.SpecificationFile = ofd.FileName;
              _conversionWorkflow.InvalidateProcessing();
              UpdateControls();

              Properties.Settings.Default.LastSpecificationFile = ofd.FileName;
              Properties.Settings.Default.Save();
           }
        }
    }
}
